/**   
*    
* @file
* @brief
*
* @<b>文件名</b>      : TopicInfoList
* @n@n<b>版权所有</b>: 网龙应用软件开发一部
* @n@n<b>作  者</b>  : Administrator
* @n@n<b>创建时间</b>: 2014-2-7 下午3:40:37 
* @n@n<b>文件描述</b>:  
* @version  
*/
package com.common.weibolib;

import java.util.ArrayList;

import com.common.android.utils.parser.BaseType;

/**
 * ClassName:TopicInfoList
 * Function: TODO ADD FUNCTION
 * Reason:	 TODO ADD REASON
 * 
 * @author   chenqian		
 * @version  
 * @since    Ver 1.1
 * @Date	 2014	2014-2-7		下午3:40:37
 *
 * @see 	 
 */

public class TopicInfoList extends ArrayList<TopicInfo> implements BaseType{
	public static final int TYPE_FRIENDS_TIME_LINE=0;	//某个用户及其关注的朋友的微博
	public static final int TYPE_USER=1;				//某个用户的微博
	public static final int TYPE_PUBLIC=2;				//公共微博
    private static final long serialVersionUID = 1L;
    
    public int mTotal;

    /**
     * 比列表位置都小
     */
    public final static int ADDPOS_LESS = -1;

    /**
     * 比列表位置都大 
     */
    public final static int ADDPOS_GREATER = -5;

    /**
     * 重复数据
     */
    public final static int ADDPOS_CONTAINS = -9;

    /**
     * 获取新对象的插入位置
     * 
     * @return
     */
    public int getAddPosOfItem(TopicInfo ti) {
        int size = this.size();
        if (size == 0) {
            return 0;
        }
        long timestamp_add = ti.post_time;

        // 比列表的时间都新
        if (timestamp_add > this.get(0).post_time) {
            return ADDPOS_LESS;
        }
        // 比列表的时间都旧
        if (timestamp_add < this.get(this.size() - 1).post_time) {
            return ADDPOS_GREATER;
        }

        for (int i = 0; i < size ; i++) {
            TopicInfo curti = this.get(i);
            if (timestamp_add > curti.post_time) {
                return i;
            }
        }

        return ADDPOS_GREATER;
    }
    
    /**
     * 根据ID删除对象  
     * 
     * @param ID
     */
    public void removeItemByID(long ID) {
        for (TopicInfo ti : this) {
            if (ID == ti.tid) {
                this.remove(ti);
                break;
            }
        }
    }
    
    /**
     * 获取某个时间戳的微博ID
     * 
     * @param timestamp
     * @return
     */
    public long getTweetIDByTimestamp(long timestamp) {
        for (TopicInfo ti : this) {
            if (ti.post_time == timestamp) {
                return ti.tid;
            }
        }
        return this.get(size()-1).tid;
    }
    
    /**
     * 获取大于某个时间戳的微博ID
     * 
     * @param timestamp
     * @return
     */
    public long getNewerTweetIDByTimestamp(long timestamp) {
        for (TopicInfo ti : this) {
            if (ti.post_time > timestamp) {
                return ti.post_time;
            }
        }
        return 0;
    }
    
    /**
     * 获取Tweet对应的Index 
     * 
     * @param tweetID
     * @return
     */
    public int indexOfTweet(long tweetID) {
        for (TopicInfo ti : this) {
            if (ti.tid == tweetID) {
                return this.indexOf(ti);
            }
        }
        return 0;
    }
    
    /**
     * 获取子列表  
     * 
     * @return
     */
    public TopicInfoList subList(int start, int end) {
        if (end > this.size()) {
            end = this.size();
        }
        TopicInfoList result = new TopicInfoList();
        result.addAll(super.subList(start, end));
        return result;
    }
    
    /**
     * 根据tweetID获取sinaID  
     * 
     * @param tweetID
     * @return
     */
    public long getSinaID(long tweetID) {
        for (TopicInfo ti : this) {
            if (ti.tid == tweetID) {
                return ti.weibo_tid;
            }
        }
        return 0;
    }
    
    /**
     * 根据ID判断是否纯粹新浪微博  
     * 
     * @param tweetID
     * @return
     */
    public boolean isOnlySina(long tweetID) {
        for (TopicInfo ti : this) {
            if (ti.tid == tweetID) {
                return ti.isOnlySina;
            }
        }
        return false;
    }
    
    /**
     * @n<b>函数名称</b>     :deleteById
     * @brief
     * @see
     * @param  @param sinaID 发送企业微博时同步到sina微博的id
     * @param  @param sameTweetInNd 原企业微博
     * @return void
     * @<b>作者</b>          :  cb
     * @<b>创建时间</b>      :  2013-3-22上午11:16:11      
    */
    public void deleteById(long sinaID,TopicInfo orcTweetInNd){
    	for (TopicInfo ti : this) {
            if (ti.tid == sinaID) {
                this.remove(ti);
                return;
            }
        }
    }

}
